package 设计模式;

import java.util.Scanner;

/**
 * @author shy_black
 * @date 2019/3/9 20:25
 * @Description: 简单工厂：
 * 将客户端的new操作解耦到第三方(工厂类)
 * 一个抽象产品类
 * 多个具体产品类
 * 一个工厂（在此new）
 * 客户端通过工厂类获取具体实例
 *
 */

interface Computer {
    void printComputer();
}

class MacBook implements Computer {

    @Override
    public void printComputer() {
        System.out.println("This is a Macbook.");
    }
}

class SurfaceBook implements Computer {

    @Override
    public void printComputer() {
        System.out.println("This is a surfaceBook");
    }
}

class Dell implements Computer {
    @Override
    public void printComputer() {
        System.out.println("This is a Dell");
    }

}

//工具方法，用static修饰，并将工具类的构造方法用private封装
//防止产生实例化对象
class ComputerFactory {
    private ComputerFactory() {
    }

    public static Computer getInstance(String str) {
        Computer computer = null;
        if (str.equals("mac")) {
            computer = new MacBook();
        } else if (str.equals("surface")) {
            computer = new SurfaceBook();
        } else if (str.equals("dell")) {
            computer = new Dell();
        }
        return computer;

    }
}

public class 简单工厂模式 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要的电脑型号：");
        String type = scanner.nextLine();
        Computer computer = ComputerFactory.getInstance(type);
        computer.printComputer();
    }
}



